from __future__ import annotations

from typing import Optional
from pymongo import MongoClient
from pymongo.collection import Collection
from pymongo.database import Database

from quantcore.config import CONFIG


class Mongo:
    _client: Optional[MongoClient] = None
    _db: Optional[Database] = None

    @classmethod
    def client(cls) -> MongoClient:
        """返回全局 MongoClient，按需懒加载。"""
        if cls._client is None:
            cls._client = MongoClient(CONFIG.mongo.mongo_uri)
        return cls._client

    @classmethod
    def db(cls) -> Database:
        """返回默认数据库句柄。"""
        if cls._db is None:
            cls._db = cls.client()[CONFIG.mongo.database]
        return cls._db

    @classmethod
    def col(cls, name: str) -> Collection:
        """快捷获取集合句柄。"""
        return cls.db()[name]




